﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nwrole.toolbox.sync.Utils
{
    public static class DbTypeUtil
    {
        public static SyncMapColumnType MakeTsqlType(string COLUMN_NAME, SyncMapColumnType SourceSqlType, string DATA_TYPE, string NULLABLE, string DATA_LENGTH, string DATA_PRECISION, string DATA_SCALE)
        {
            SyncMapColumnType result = new SyncMapColumnType();
            if ("INTEGER".Equals(SourceSqlType.TypeName))
            {
                result.TypeName = "int";
            }
            else if ("NUMBER".Equals(DATA_TYPE))
            {
                if ((DATA_PRECISION != "") && (DATA_PRECISION != ""))
                {
                    result.TypeName = string.Format("decimal({0},{1})", DATA_PRECISION, DATA_SCALE);
                }
                else
                {
                    result.TypeName = string.Format("decimal");
                }
            }
            else if ("VARCHAR2".Equals(DATA_TYPE))
            {
                result.TypeName = string.Format("nvarchar({0})", DATA_LENGTH);
            }
            else if ("CHAR".Equals(DATA_TYPE))
            {
                result.TypeName = string.Format("char({0})", DATA_LENGTH);
            }
            else if ("DATE".Equals(DATA_TYPE))
            {
                result.TypeName = "datetime2(7)";
            }
            else if ("LONG".Equals(DATA_TYPE))
            {
                result.TypeName = "bigint";
            }
            else if ("BOOL".Equals(DATA_TYPE))
            {
                result.TypeName = "bigint";
            }
            result.TypeNullable = SourceSqlType.TypeNullable;
            return result;
        }

        public static string MakeCSharpType(string dataType)
        {
            if ("INTEGER".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Int32";
            }
            if ("INT".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Int32";
            }
            if ("SMALLINT".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Int16";
            }
            if ("BIGINT".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Int64";
            }
            if ("NUMBER".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Decimal";
            }
            if ("MONEY".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Decimal";
            }
            if ("FLOAT".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Decimal";
            }
            if ("DECIMAL".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Decimal";
            }
            if ("VARCHAR2".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.String";
            }
            if ("VARCHAR".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.String";
            }
            if ("NVARCHAR".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.String";
            }
            if ("CHAR".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.String";
            }
            if ("NCHAR".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.String";
            }
            if ("TEXT".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.String";
            }
            if ("DATE".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.DateTime";
            }
            if ("DATETIME".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.DateTime";
            }
            if ("LONG".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Long";
            }
            if ("UNIQUEIDENTIFIER".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Guid";
            }
            if ("TIMESTAMP".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Data.Linq.Binary";
            }
            if ("BIT".Equals(dataType, StringComparison.CurrentCultureIgnoreCase))
            {
                return "System.Boolean";
            }
            return null;
        }

        public static SyncMapColumnType MakeOracleType(string COLUMN_NAME, string DATA_TYPE, string NULLABLE, string DATA_LENGTH, string DATA_PRECISION, string DATA_SCALE)
        {
            SyncMapColumnType result = new SyncMapColumnType();
            if ("INTEGER".Equals(DATA_TYPE))
            {
                result.TypeName = "INTEGER";
            }
            else if ("NUMBER".Equals(DATA_TYPE))
            {
                if (("22".Equals(DATA_LENGTH)) && ("0".Equals(DATA_SCALE)))
                {
                    result.TypeName = "INTEGER";
                }
                else if (("22".Equals(DATA_LENGTH)) && ("".Equals(DATA_SCALE)))
                {
                    result.TypeName = "NUMBER";
                }
                else
                {
                    result.TypeName = string.Format("NUMBER({0},{1})", DATA_PRECISION, DATA_SCALE);
                }
            }
            else if ("VARCHAR2".Equals(DATA_TYPE))
            {
                result.TypeName = string.Format("VARCHAR2({0} BYTE)", DATA_LENGTH);
            }
            else if ("CHAR".Equals(DATA_TYPE))
            {
                result.TypeName = string.Format("CHAR({0} BYTE)", DATA_LENGTH);
            }
            else if ("DATE".Equals(DATA_TYPE))
            {
                result.TypeName = "DATE";
            }
            else if ("LONG".Equals(DATA_TYPE))
            {
                result.TypeName = "VARCHAR2(4000 BYTE)";
            }
            else
            {
                result.TypeName = "missing " + DATA_TYPE;
            }
            result.TypeNullable = ("Y".Equals(NULLABLE));

            return result;
        }
    }
}
